
import { ApplicationCategory, Application } from '../types';

import { API_BASE_URL, getToken, request } from '../../utils';
import { G01_FORM_SCHEMA_CLASS_IDENTIFIER, G01_MODEL_PROVIDER_CLASS_IDENTIFIER } from './constants';
import { dsFind, dsFindOne, dsInsert, dsUpdateById, dsUpdateByKeys, ComparsionOperator, dsBatchInsert } from '../local';
import { FORM_SCHEMA_CLASS_IDENTIFIER, FORM_SCHEMA_ITEM_CLASS_IDENTIFIER, MODEL_CLASS_IDENTIFIER, MODEL_PROVIDER_CLASS_IDENTIFIER, MODEL_PROVIDER_SETTING_CLASS_IDENTIFIER, MODEL_SETTING_CLASS_IDENTIFIER, MODEL_TYPE_SETTING_CLASS_IDENTIFIER } from '../constants';
import { g01DSFind } from './datasource';

export const rrg01FetchModelProvider = async () => {
    let schemaItemExtension: any = {
        "credential_form_schemas": [
            FORM_SCHEMA_ITEM_CLASS_IDENTIFIER,
            {
                relations: [
                    {
                        conditions: {
                            related_at: 'schema_identifier'
                        }
                    }
                ]
            }
            , { class_selector: {} }
        ]
    };


    let extention = {
        class_selector: {
            "provider_credential_schema": [
                FORM_SCHEMA_CLASS_IDENTIFIER,
                {
                    relations: [
                        {
                            conditions: {
                                related_at: 'provider_form_schema'
                            }
                        }
                    ]
                }
                , { class_selector: schemaItemExtension }
            ],
            "model_credential_schema": [
                FORM_SCHEMA_CLASS_IDENTIFIER,
                {
                    relations: [
                        {
                            conditions: {
                                related_at: 'model_form_schema'
                            }
                        }
                    ]
                }
                , { class_selector: schemaItemExtension }
            ]
        }
    };

    //合并到本地中
    try {
        //
        let provider = await g01DSFind(MODEL_PROVIDER_CLASS_IDENTIFIER, {}, extention);

        try {
            await dsBatchInsert(0, provider.data.map((item) => {
                return [MODEL_PROVIDER_CLASS_IDENTIFIER, item];
            }), {
                upsert: true,
                primary_key: 'identifier'
            });
        } catch (error) {
            console.error(error);
        }
        //form schema
        let formSchemas = [];
        let formSchemaItems = [];

        for (let i = 0; i < provider.data.length; i++) {
            let provider_credential_schema = provider.data[i].provider_credential_schema[0];
            let model_credential_schema = provider.data[i].model_credential_schema[0];
            if (provider_credential_schema) {
                formSchemas.push(provider_credential_schema);
                for (let j = 0; j < provider_credential_schema.credential_form_schemas.length; j++) {
                    formSchemaItems.push(provider_credential_schema.credential_form_schemas[j]);
                }
            }
            if (model_credential_schema) {
                formSchemas.push(model_credential_schema);
                for (let j = 0; j < model_credential_schema.credential_form_schemas.length; j++) {
                    formSchemaItems.push(model_credential_schema.credential_form_schemas[j]);
                }
            }
        }
        //
        try {
            await dsBatchInsert(0, formSchemas.map((item) => {
                return [FORM_SCHEMA_CLASS_IDENTIFIER, item];
            }), {
                upsert: true,
                primary_key: 'identifier'
            });
        } catch (error) {
            console.error(error);
        }
        try {
            await dsBatchInsert(0, formSchemaItems.map((item) => {
                return [FORM_SCHEMA_ITEM_CLASS_IDENTIFIER, item];
            }), {
                upsert: true,
                primary_key: 'id'
            });
        } catch (error) {
            console.error(error);
        }
    } catch (error) {
        console.error(error);
    }
    //

    //合并到本地中
    try {
        let providerSettings = await g01DSFind(MODEL_PROVIDER_SETTING_CLASS_IDENTIFIER, {});
        await dsBatchInsert(0, providerSettings.data.map((item) => {
            return [MODEL_PROVIDER_SETTING_CLASS_IDENTIFIER, item];
        }), {
            upsert: true,
            primary_key: 'provider_identifier'
        });
    } catch (error) {
        console.error(error);

    }
    //
    //合并到本地中
    try {
        let providerModels = await g01DSFind(MODEL_CLASS_IDENTIFIER, {});

        await dsBatchInsert(0, providerModels.data.map((item) => {
            return [MODEL_CLASS_IDENTIFIER, item];
        }), {
            upsert: true,
            primary_key: 'identifier'
        });
    } catch (error) {
        console.error(error);

    }
    //
    //合并到本地中
    try {
        let providerModelSettings = await g01DSFind(MODEL_SETTING_CLASS_IDENTIFIER, {});

        await dsBatchInsert(0, providerModelSettings.data.map((item) => {
            return [MODEL_SETTING_CLASS_IDENTIFIER, item];
        }), {
            upsert: true,
            primary_key: 'identifier'
        });
    } catch (error) {
        console.error(error);

    }
    return;
}